package com.xzbd.gef.controller;


import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.xzbd.core.gef.Node;
import com.xzbd.editors.MultiLineCellEditor;
import com.xzbd.gef.model.text.NotesNode;
import com.xzbd.gef.model.text.TextNode;
import com.xzbd.gef.policy.AppDeletePolicy;
import com.xzbd.gef.view.TextNodeFigure;
import com.xzbd.listeners.PropListener;

public class TextNodePart extends AppAbstractEditPart implements NodeEditPart {

	
	
	private DirectEditManager directManager;

//	IFigure figure  = null;
	@Override
	protected IFigure createFigure() {
		// TODO Auto-generated method stub
		IFigure figure = new TextNodeFigure();
		return figure;
	}

	@Override
	protected void createEditPolicies() {
		// TODO Auto-generated method stub
		// installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new NodeDirectEditPolicy());
		super.createEditPolicies();
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new AppDeletePolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new TextDirectEditPolicy());
	}

	public void refreshVisuals() {
		super.refreshVisuals();
		TextNodeFigure figure = (TextNodeFigure) getFigure();

//		this.figure = figure.getParent();
		TextNode model = (TextNode) getModel();

		figure.setNotes(model.getNotes());
		figure.setLayout(model.getLayout());
		figure.setColor(model.getColor());
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if (evt.getPropertyName().equals(PropListener.PROPERTY_COLOR))
			refreshVisuals();
		if (evt.getPropertyName().equals(PropListener.PROPERTY_LAYOUT))
			refreshVisuals();
		if (evt.getPropertyName().equals(NotesNode.PROPERTY_NOTES))
			refreshVisuals();
	}

	public void performRequest(Request req) {
		if (req.getType().equals(RequestConstants.REQ_OPEN)) {
			try {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				page.showView(IPageLayout.ID_PROP_SHEET);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
		if (req.getType().equals(RequestConstants.REQ_DIRECT_EDIT) || req.getType().equals(RequestConstants.REQ_OPEN)) {
			textPerformDirectEdit();

			System.out.println("int textnodepart, performRequest.");
			return;
		}
		// super.performRequest(req);
	}

	private void textPerformDirectEdit() {
		if (directManager == null) {
			directManager = new TextDirectEditManager();
		}
		directManager.show();
	}

	/**
	 * DirectEditManager
	 */
	private class TextDirectEditManager extends DirectEditManager {

		public TextDirectEditManager() {
			super(TextNodePart.this, MultiLineCellEditor.class, new TextCellEditorLocator());
		}

		protected void initCellEditor() {
			getCellEditor().setValue(((TextNode) getModel()).getNotes());
			Text text = (Text) getCellEditor().getControl();
			text.selectAll();
		}
	}

	/**
	 * CellEditorLocator
	 */
	private class TextCellEditorLocator implements CellEditorLocator {
		public void relocate(CellEditor celleditor) {
			IFigure figure = getFigure();
			Text text = (Text) celleditor.getControl();
			Rectangle rect = figure.getBounds().getCopy();
			figure.translateToAbsolute(rect);
			// text.setBounds(rect.x + 5, rect.y + 5, rect.width - 5, rect.height - 5);
			text.setBounds(rect.x + 5, rect.y + 5, 180, 50);
		}
	}

	/**
	 * DirectEditCommand
	 */
	private class TextDirectEditCommand extends Command {

		private String oldName;

		private String newName;

		public void execute() {
			TextNode model = (TextNode) getModel();
			oldName = model.getNotes();
			model.setNotes(newName);
		}

		public void setName(String name) {
			newName = name;
		}

		public void undo() {
			TextNode model = (TextNode) getModel();
			model.setNotes(oldName);
		}
	}

	/**
	 * DirectEditPolicy
	 */
	private class TextDirectEditPolicy extends DirectEditPolicy {

		protected Command getDirectEditCommand(DirectEditRequest request) {
			TextDirectEditCommand command = new TextDirectEditCommand();
			command.setName((String) request.getCellEditor().getValue());
			return command;
		}

		protected void showCurrentEditValue(DirectEditRequest request) {
		}
	}

	public List<Node> getModelChildren() {
		return new ArrayList<Node>();
	}

	@Override
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ConnectionAnchor getSourceConnectionAnchor(Request arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ConnectionAnchor getTargetConnectionAnchor(Request arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
